package com.cms.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cms.domain.Feedback;
import com.cms.entity.Slide;
import com.cms.service.SlideService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 幻灯片
 * 
 * 
 * 
 */
@Controller("adminSlideController")
@RequestMapping("/admin/slide")
public class SlideController extends BaseController {


	@Autowired
	private SlideService slideService;
	
	/**
	 * 列表
	 */
	@RequestMapping
	public String index(Integer pageNumber, ModelMap modelMap){
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", slideService.findPage(pageNumber,PAGE_SIZE,getCurrentSite().getId()));
		modelMap.addAttribute("gids", slideService.findGids(getCurrentSite().getId()));
		return getView("slide/index");
	}


	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("gids", slideService.findGids(getCurrentSite().getId()));
		return getView("slide/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Slide slide) {
		if(slide.getGid()==null){
			Integer maxGid = slideService.findMaxGid(getCurrentSite().getId());
			if(maxGid==null){
				maxGid=0;
			}
			maxGid = maxGid+1;
			slide.setGid(maxGid);
		}
		slide.setSiteId(getCurrentSite().getId());
		slide.setCreateDate(LocalDateTime.now());
		slide.setUpdateDate(LocalDateTime.now());
		slideService.save(slide);
		return "redirect:"+getListQuery("/admin/slide");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("slide", slideService.getById(id));
		modelMap.addAttribute("gids", slideService.findGids(getCurrentSite().getId()));
		return getView("slide/edit");
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public String update(Slide slide) {
		slide.setUpdateDate(LocalDateTime.now());
		slideService.updateById(slide);
		return "redirect:"+getListQuery("/admin/slide");
	}
	
	/**
	 * 修改排序
	 */
	@ResponseBody
	@RequestMapping("/updateSort")
	public Feedback updateSort(String sortArray){
	    JSONArray jsonArray = JSONArray.parseArray(sortArray);
	    for(int i=0;i<jsonArray.size();i++){
	        JSONObject jsonObject = jsonArray.getJSONObject(i);
			Integer id = jsonObject.getInteger("id");
	        Integer sort = jsonObject.getInteger("sort");
	        Slide slide = slideService.getById(id);
	        slide.setSort(sort);
			slideService.updateById(slide);
	    }
	    return Feedback.success(new HashMap<>());
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			slideService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}